using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" JurnalConfig to change the class name "JurnalConfigServices" in code, svc and config file together.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class JurnalConfigServices : IJurnalConfigServices
    {
        private readonly JurnalConfigRepository JurnalConfigRepository;

        public JurnalConfigServices()
        {
            JurnalConfigRepository = new JurnalConfigRepository(new WcfSessionStorage());
        }

        #region IJurnalConfigServices Members

        public List<JurnalConfigDTO> GetList(int page, out long recordCount, string sortProperty = null,
                                             string sortType = "ASC")
        {
            var JurnalConfigs = new List<JurnalConfig>();
            var Results = new List<JurnalConfigDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                JurnalConfigs = JurnalConfigRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                JurnalConfigs = JurnalConfigRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            recordCount = JurnalConfigRepository.RecordCount;

            JurnalConfigs.ForEach(x =>
                                      {
                                          var JurnalConfigDto = new JurnalConfigDTO();
                                          Mapper.Map(x, JurnalConfigDto);
                                          //AutoMapper.Mapper.Map(x.ParentJurnalConfig, JurnalConfigDto.ParentJurnalConfig);
                                          Results.Add(JurnalConfigDto);
                                      }
                );

            return Results;
        }

        public bool Save(JurnalConfigDTO JurnalConfigDTO)
        {
            bool res;
            try
            {
                JurnalConfig JurnalConfig =
                    JurnalConfigRepository.GetDataOne(x => x.JurnalConfigId == JurnalConfigDTO.JurnalConfigId);

                if (JurnalConfig == null)
                {
                    JurnalConfig = new JurnalConfig();
                }
                Mapper.Map(JurnalConfigDTO, JurnalConfig);
                JurnalConfigRepository.SaveOrUpdate(JurnalConfig);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public bool Delete(JurnalConfigDTO JurnalConfigDto)
        {
            bool res;
            try
            {
                var JurnalConfig = new JurnalConfig();
                Mapper.Map(JurnalConfigDto, JurnalConfig);
                JurnalConfigRepository.Delete(JurnalConfig);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public List<JurnalConfigDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                                       out long recordCount)
        {
            var JurnalConfigs = new List<JurnalConfig>();
            var Results = new List<JurnalConfigDTO>();
            JurnalConfigs = JurnalConfigRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = JurnalConfigRepository.RecordCount;
            JurnalConfigs.ForEach(x =>
                                      {
                                          var JurnalConfigDto = new JurnalConfigDTO();
                                          Mapper.Map(x, JurnalConfigDto);
                                          Results.Add(JurnalConfigDto);
                                      }
                );

            return Results;
        }


        public List<JurnalConfigDTO> GetAllJurnalConfig()
        {
            List<JurnalConfig> JurnalConfigs = JurnalConfigRepository.FetchAll().ToList();
            var Results = new List<JurnalConfigDTO>();
            JurnalConfigs.ForEach(x =>
                                      {
                                          var JurnalConfigDto = new JurnalConfigDTO();
                                          Mapper.Map(x, JurnalConfigDto);
                                          Results.Add(JurnalConfigDto);
                                      }
                );

            return Results;
        }


        public JurnalConfigDTO GetJurnalConfig(int JurnalConfigId)
        {
            JurnalConfig JurnalConfig = JurnalConfigRepository.GetDataOne(x => x.JurnalConfigId == JurnalConfigId);
            var JurnalConfigDto = new JurnalConfigDTO();
            Mapper.Map(JurnalConfig, JurnalConfigDto);
            return JurnalConfigDto;
        }


        public List<String> GetJurnalTypes()
        {
            var res = new List<String>();
            res.Add(typeof(PurchaseInvoice).ToString());
            res.Add(typeof(PurchaseReturn).ToString());
            res.Add(typeof(BTB).ToString());
            res.Add(typeof(BtbElse).ToString());
            res.Add(typeof(SalesInvoice).ToString());
            res.Add(typeof(DeliveryOrder).ToString());
            res.Add(typeof(SalesReturn).ToString());
            res.Add(typeof(InventoryAdjustment).ToString());
            res.Add(typeof(DeliveryOrderElse).ToString());
            return res.OrderBy(x => x).ToList();
        }

        public List<String> GetProperty(string TypeName)
        {
            Assembly Assem = Assembly.Load("Core");
            var res = new List<String>();

            //foreach (Type type in Assem.GetTypes())
            //{
            //    if (type.Name.Length < TypeName.Length) continue;
            //    if (type.Name == TypeName || type.Name.Substring(0, TypeName.Length) == TypeName)
            //    {
                    foreach (PropertyInfo property in Type.GetType(TypeName).GetProperties())
                    {
                        if (property.PropertyType == typeof (Decimal) || property.PropertyType == typeof (Double))
                            res.Add(property.Name);
                    }
            //    }
            //}

            return res;
        }

        #endregion
    }
}